home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / WinCE / SDKWindowsCE / HandHeldPCPro30 / sdk.exe / Jupiter SDK / data1.cab / MFC_Samples / ctrltest / bbutton.cpp next >
Encoding:
C/C++ Source or Header  |  1999-02-19  |  6.9 KB  |  278 lines

  1. // bbutton.cpp : bitmap button test
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1999 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "ctrltest.h"
  15.  
  16. /////////////////////////////////////////////////////////////////////////////
  17. // BitmapButton Test dialog #1
  18.  
  19. // In this example we pass the bitmap resource names to LoadBitmaps
  20. //  OnInitDialog is used to Subclass the buttons so the dialog
  21. //  controls get attached to the MFC WndProc for C++ message map dispatch.
  22.  
  23. class CBMTest1Dlg : public CDialog
  24. {
  25. protected:
  26.     CBitmapButton button1, button2;
  27.     CWnd*          m_FocusWnd;
  28. public:
  29.     //{{AFX_DATA(CBMTest1Dlg)
  30.         enum { IDD = IDM_TEST_BITMAP_BUTTON1 };
  31.     //}}AFX_DATA
  32.         CBMTest1Dlg();
  33.  
  34.     BOOL OnInitDialog();
  35.     //{{AFX_MSG(CBMTest1Dlg)
  36.     afx_msg void OnActivate( UINT nState, CWnd* pWndOther, BOOL bMinimized );
  37.     //}}AFX_MSG
  38.     DECLARE_MESSAGE_MAP()
  39. };
  40.  
  41. BEGIN_MESSAGE_MAP(CBMTest1Dlg, CDialog)
  42.     //{{AFX_MSG_MAP(CBMTest1Dlg)
  43. //    ON_MESSAGE(WM_ACTIVATE, OnActivate)
  44.     ON_WM_ACTIVATE()
  45.     //}}AFX_MSG_MAP
  46. END_MESSAGE_MAP()
  47.  
  48.  
  49. CBMTest1Dlg::CBMTest1Dlg()
  50.     : CDialog(CBMTest1Dlg::IDD)
  51. {
  52.     // NOTE: The obsolete MFC V1 CBitmapButton constructor with 3 arguments is
  53.     //  replaced by a call to LoadBitmaps.
  54.     if (!button1.LoadBitmaps(_T("Image1Up"), _T("Image1Down"), _T("Image1Focus")) ||
  55.         !button2.LoadBitmaps(_T("Image2Up"), _T("Image2Down"), _T("Image2Focus")))
  56.     {
  57.         TRACE0("Failed to load bitmaps for buttons\n");
  58.         AfxThrowResourceException();
  59.     }
  60.     m_FocusWnd = NULL;
  61. }
  62.  
  63. BOOL CBMTest1Dlg::OnInitDialog()
  64. {
  65.     // each dialog control has special bitmaps
  66.     VERIFY(button1.SubclassDlgItem(IDOK, this));
  67.     button1.SizeToContent();
  68.     VERIFY(button2.SubclassDlgItem(IDCANCEL, this));
  69.     button2.SizeToContent();
  70.  
  71.     return TRUE;
  72. }
  73.  
  74. void CBMTest1Dlg::OnActivate( UINT nState, CWnd* pWndOther, BOOL bMinimized )
  75. {
  76. /*    if(nState == WA_ACTIVE)
  77.     {
  78.         InvalidateRect(NULL);
  79.         UpdateWindow();
  80.  
  81.         if(m_FocusWnd)
  82.             ::SetFocus(m_FocusWnd->m_hWnd);
  83.     }
  84.     else if(nState == WA_INACTIVE)
  85.         m_FocusWnd = GetFocus();*/
  86. }
  87.  
  88. /////////////////////////////////////////////////////////////////////////////
  89. // BitmapButton Test dialog #2
  90.  
  91. // In this example we use the CBitmapButton AutoLoad member function.
  92. //  Autoload uses the text/title of the button as the base resource name.
  93. //  For this trivial example the buttons are called "OK" and "CANCEL",
  94. //  which use the bitmaps "OKU", "OKD", "OKF", "CANCELU", "CANCELD"
  95. //  and "CANCELF" respectively for the up, down and focused images.
  96.  
  97. #define ID_BUTTON_MIN       IDOK
  98. #define N_BUTTONS   (IDCANCEL - ID_BUTTON_MIN + 1)
  99.  
  100. class CBMTest2Dlg : public CDialog
  101. {
  102. protected:
  103.     // array of buttons constructed with no attached bitmap images
  104.     CBitmapButton buttons[N_BUTTONS];
  105.     CWnd*          m_FocusWnd;
  106. public:
  107.     //{{AFX_DATA(CBMTest2Dlg)
  108.         enum { IDD = IDM_TEST_BITMAP_BUTTON2 };
  109.     //}}AFX_DATA
  110.     CBMTest2Dlg()
  111.         : CDialog(CBMTest2Dlg::IDD)
  112.         { m_FocusWnd = NULL; }
  113.  
  114.     BOOL OnInitDialog();
  115.     //{{AFX_MSG(CBMTest2Dlg)
  116.     afx_msg void OnActivate( UINT nState, CWnd* pWndOther, BOOL bMinimized );
  117.     //}}AFX_MSG
  118.     DECLARE_MESSAGE_MAP()
  119. };
  120.  
  121. BEGIN_MESSAGE_MAP(CBMTest2Dlg, CDialog)
  122.     //{{AFX_MSG_MAP(CBMTest2Dlg)
  123. //    ON_MESSAGE(WM_ACTIVATE, OnActivate)
  124.     ON_WM_ACTIVATE()
  125.     //}}AFX_MSG_MAP
  126. END_MESSAGE_MAP()
  127.  
  128.  
  129. BOOL CBMTest2Dlg::OnInitDialog()
  130. {
  131.     // load bitmaps for all the bitmap buttons (does SubclassButton as well)
  132.     for (int i = 0; i < N_BUTTONS; i++)
  133.         VERIFY(buttons[i].AutoLoad(ID_BUTTON_MIN + i, this));
  134.     return TRUE;
  135. }
  136.  
  137.  
  138. void CBMTest2Dlg::OnActivate( UINT nState, CWnd* pWndOther, BOOL bMinimized )
  139. {
  140.     if(nState == WA_ACTIVE)
  141.     {
  142.         InvalidateRect(NULL);
  143.         UpdateWindow();
  144.  
  145.         if(m_FocusWnd)
  146.             ::SetFocus(m_FocusWnd->m_hWnd);
  147.     }
  148.     else if(nState == WA_INACTIVE)
  149.         m_FocusWnd = GetFocus();
  150. }
  151.  
  152.  
  153. /////////////////////////////////////////////////////////////////////////////
  154. // BitmapButton Test dialog #3
  155.  
  156. // This is an extension of test dialog 2 using AutoLoad using the disabled
  157. //   state with the "X" suffix.
  158. // Here we use bitmap buttons to select a number between 1 and 10.
  159. // The "PREV" and "NEXT" buttons change the number.  These buttons are
  160. //  disabled when the number hits the limits.
  161.  
  162. class CBMTest3Dlg : public CDialog
  163. {
  164. protected:
  165.     // construct
  166.     CBitmapButton okButton;
  167.     CBitmapButton prevButton;
  168.     CBitmapButton nextButton;
  169.     CWnd*          m_FocusWnd;
  170.  
  171. public:
  172.     int m_nNumber;
  173.     //{{AFX_DATA(CBMTest3Dlg)
  174.         enum { IDD = IDM_TEST_BITMAP_BUTTON3 };
  175.     //}}AFX_DATA
  176.  
  177.     CBMTest3Dlg()
  178.         : CDialog(CBMTest3Dlg::IDD)
  179.         { m_FocusWnd = NULL; }
  180.  
  181.     BOOL OnInitDialog();
  182.     void Update();
  183.  
  184.     //{{AFX_MSG(CBMTest3Dlg)
  185.     afx_msg void OnActivate( UINT nState, CWnd* pWndOther, BOOL bMinimized );
  186.     afx_msg void OnNextNumber();
  187.     afx_msg void OnPrevNumber();
  188.     //}}AFX_MSG
  189.     DECLARE_MESSAGE_MAP()
  190. };
  191.  
  192. BOOL CBMTest3Dlg::OnInitDialog()
  193. {
  194.     // load bitmaps for all the bitmap buttons (does SubclassButton as well)
  195.     VERIFY(okButton.AutoLoad(IDOK, this));
  196.     VERIFY(prevButton.AutoLoad(ID_PREV, this));
  197.     VERIFY(nextButton.AutoLoad(ID_NEXT, this));
  198.     Update();
  199.     return TRUE;
  200. }
  201.  
  202. BEGIN_MESSAGE_MAP(CBMTest3Dlg, CDialog)
  203.     //{{AFX_MSG_MAP(CBMTest3Dlg)
  204. //    ON_MESSAGE(WM_ACTIVATE, OnActivate)
  205.     ON_WM_ACTIVATE()
  206.     ON_COMMAND(ID_PREV, OnPrevNumber)
  207.     ON_COMMAND(ID_NEXT, OnNextNumber)
  208.     //}}AFX_MSG_MAP
  209. END_MESSAGE_MAP()
  210.  
  211. void CBMTest3Dlg::OnPrevNumber()
  212. {
  213.     m_nNumber--;
  214.     Update();
  215. }
  216.  
  217. void CBMTest3Dlg::OnNextNumber()
  218. {
  219.     m_nNumber++;
  220.     Update();
  221. }
  222.  
  223. void CBMTest3Dlg::Update()
  224. {
  225.     ::SetDlgItemInt(m_hWnd, IDC_NUMBEROUT, m_nNumber, TRUE);
  226.     prevButton.EnableWindow(m_nNumber > 1);
  227.     nextButton.EnableWindow(m_nNumber < 10);
  228.     // move focus to active button
  229.     if (!prevButton.IsWindowEnabled())
  230.         nextButton.SetFocus();
  231.     else if (!nextButton.IsWindowEnabled())
  232.         prevButton.SetFocus();
  233. }
  234.  
  235. void CBMTest3Dlg::OnActivate( UINT nState, CWnd* pWndOther, BOOL bMinimized )
  236. {
  237.     if(nState == WA_ACTIVE)
  238.     {
  239.         InvalidateRect(NULL);
  240.         UpdateWindow();
  241.  
  242.         if(m_FocusWnd)
  243.             ::SetFocus(m_FocusWnd->m_hWnd);
  244.     }
  245.     else if(nState == WA_INACTIVE)
  246.         m_FocusWnd = GetFocus();
  247. }
  248.  
  249. /////////////////////////////////////////////////////////////////////////////
  250. // Test driver routines
  251.  
  252. void CTestWindow::OnTestBitmapButton1()
  253. {
  254.     CBMTest1Dlg dlg;
  255.     dlg.DoModal();
  256. }
  257.  
  258. void CTestWindow::OnTestBitmapButton2()
  259. {
  260.     CBMTest2Dlg dlg;
  261.     dlg.DoModal();
  262. }
  263.  
  264. void CTestWindow::OnTestBitmapButton3()
  265. {
  266.     CBMTest3Dlg dlg;
  267.     dlg.m_nNumber = 5;
  268.     dlg.DoModal();
  269.  
  270.     CString strYouChose;
  271.     strYouChose.LoadString(IDS_YOU_CHOSE);
  272.     CString strMsg;
  273.     strMsg.Format(strYouChose, dlg.m_nNumber);
  274.     AfxMessageBox(strMsg);
  275. }
  276.  
  277. /////////////////////////////////////////////////////////////////////////////
  278.